package com.teamone.etl.datasource.timing;

import com.teamone.etl.datasource.entity.DataSource;
import com.teamone.etl.datasource.pool.JdbcConnector;
import com.teamone.etl.datasource.pool.RedisConnector;
import com.teamone.etl.datasource.service.DataSourceService;
import javax.annotation.Resource;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author 1752140@qq.com
 * 2021/1/7
 */
@Component
public class DataSourceTimer {

    @Resource
    private DataSourceService dataSourceService;

    @Scheduled(cron = "0 0/1 * * * ?")
    public void dataSourceTiming(){
        JdbcConnector.JDBC_POOL_MAP.forEach((key,connection) -> {
            DataSource dataSource = dataSourceService.getDataSourceById(key);
            if(!JdbcConnector.isConnected(dataSource)){
                JdbcConnector.put(dataSource);
                System.out.println("id="+key+"重连");
            }
        });
        RedisConnector.redisPoolMap.forEach((key,jedis) -> {
            if (!jedis.isConnected()){
                RedisConnector.getRedisConnection(dataSourceService.getDataSourceById(key));
                System.out.println("id="+key+"重连");
            }
        });
    }


}
